/*
NOTES TO DATA ANALYSIS:
	1. Treatment effect regressions (each regression should control for strata variables)
		- by pooled treatment
		- by 5 frames
		- by 2 timings
		- by 10 arms
*/

clear all
set more off
capture log close

********************************************************************************

log using "$logfiles/analysis_key_itt_`c(current_date)'.log", replace

use "$covidclean/smscovid_clean.dta", clear

***Keep relevant observations: consent taken and above 18 years of age, non-missing treatment arms
	keep if consent==1 & age>=18
	keep if ~missing(treatment_arm)
	count //3964
	
***HOUSEKEEPING - BEHAVIOR TREATMENTS

	***Create dummies with treatment arms interacted with behavior
		foreach v in $treat_pool $treat_frames $treat_timings $treat_arms {
			gen `v'_sd = `v'*behavior_sd
			gen `v'_hw = `v'*behavior_hw
		}
	
***REGRESSIONS

	foreach v of varlist risk_sick_h risk_die_h {
			
				*Regression
				reghdfe `v' $treat_pool_sd $treat_pool_hw if $sample, a($studycontrols $covariates) vce(robust)

				mat b = r(table)
				local tcount: word count $treat_pool_sd $treat_pool_hw				
				forval i = 1/`tcount' {
					local b_`i' = b[1,`i'] //beta
					local se_`i' = b[2,`i'] //asymptotic s.e.
					local a_p_`i' = b[4,`i'] //asymptotic p-value
					local p_`i' = 0 //exact p-value
				}
				
				*Randomization inference using RI dataset created
				preserve
    				forval batch = 1/$nbatch {
					   use if batch == `batch' using "$rianalysis/ri_data_final", clear
    				   di "Running Batch `batch' for variable `v'"
    				   qui reghdfe `v' $treat_pool_sd $treat_pool_hw if $sample, a($studycontrols $covariates) vce(robust) 
    				   mat ri_b = r(table)
    				   forval i = 1/`tcount' {
            			   local ri_b_`i' = ri_b[1,`i']
            			   *One-tailed comparison
				           if `ri_b_`i'' >= `b_`i'' local p_`i' = `p_`i'' + 1 
					    }  
				    } 
				restore
				
				*Compute Exact p-values 
				forval i = 1/`tcount' {
					local p_`i' = `p_`i''/$nbatch
				}
				
				*Imputing coefficients, asymptotic and exact p-values in the output matrix
				mat `v'_p = J(`tcount',3,.)
				forval i = 1/`tcount' {
					mat `v'_p[`i',1] = round(`b_`i'',0.001)
					mat `v'_p[`i',2] = round(`se_`i'',0.001)
					mat `v'_p[`i',3] = round(`p_`i'',0.001)
				}
				
				*Save matrix
				matsave `v'_p, replace saving path($tables)
				
	}
